/**
 * 
 */
package com.cqan.tuan.parser;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.dom4j.Document;
import org.dom4j.Element;
import org.springframework.stereotype.Component;

import com.cqan.exception.AppException;
import com.cqan.tuan.entity.BaseEntity;
import com.cqan.tuan.entity.Goods;
import com.cqan.tuan.entity.Shop;
import com.cqan.tuan.service.GoodsService;
import com.google.common.collect.Lists;

/**
 * @author slaton
 *
 * 2012-12-1上午11:47:30 created
 */
@Component
public class Hao123XmlParser extends XmlParser {

	@Resource
	private GoodsService goodsService;
	/**
	 * hao123模版解析(参照拉手网)
	 * http://open.client.lashou.com/api/detail/city/2419/p/1/r/10
	 */
	@Override
	@SuppressWarnings("unchecked")
	List<BaseEntity> analyze(Document doc)throws AppException {
		List<BaseEntity> goods = Lists.newArrayList();
		try {
			List<Element> elements = doc.selectNodes("urlset/url");
			if (elements!=null&&elements.size()>0) {
				for (Element e : elements) {
					Goods good = new Goods();
					good.setUrl(e.elementText("loc"));
					good.setWapUrl(e.elementText("wap_url"));
					Element data = (Element) e.selectSingleNode("data");
					Element display = (Element) data.selectSingleNode("display");
					good.setWebSite(display.elementText("website"));
					good.setCate(display.elementText("cate"));
					good.setCityName(display.elementText("city"));
					good.setWebSiteGoodId(display.elementText("gid"));
					good.setTitle(display.elementText("title"));
					good.setImage(display.elementText("image"));
					good.setSmallImage(display.elementText("small_image"));
					if(null != display.elementText("startTime") && !display.elementText("startTime").equals("")){
						good.setStartTime(Long.parseLong(display.elementText("startTime")));
					}else{
						continue;
					}
					if(null != display.elementText("endTime") && !display.elementText("endTime").equals("")){
						good.setEndTime(Long.parseLong(display.elementText("endTime")));
					}else{
						continue;
					}
					if(null != display.elementText("price") && !display.elementText("price").equals("")){
						good.setPrice(Float.parseFloat(display.elementText("price")));
					}
					if(null != display.elementText("value") && !display.elementText("value").equals("")){
						good.setOriginalPrice(Float.parseFloat(display.elementText("value")));
					}
					if(null != display.elementText("rebate") && !display.elementText("rebate").equals("")){
						good.setRebate(Float.parseFloat(display.elementText("rebate")));
					}
					if(null != display.elementText("bought") && !display.elementText("bought").equals("")){
						good.setSalesNum(Integer.parseInt(display.elementText("bought")));
					}
					good.setDescription(display.elementText("detail"));
					List<Element> shops = display.selectNodes("shops/shop");
					List<Shop> shopList = Lists.newArrayList();
					if(null != shops && shops.size()>0){
						for (Element element : shops) {
							Shop shop = new Shop();
							shop.setName(element.elementText("name"));
							shop.setTel(element.elementText("tel"));
							shop.setAddr(element.elementText("addr"));
							shop.setLongitude(element.elementText("longitude"));
							shop.setCtime(new Date());
							shop.setLatitude(element.elementText("latitude"));
							shop.setArea(element.elementText("area"));
							shopList.add(shop);
						}
					}
					good.setShops(shopList);
					goods.add(good);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		for (BaseEntity baseEntity : goods) {
			goodsService.add((Goods)baseEntity);
			System.out.println((Goods)baseEntity);
		}
		return goods;
	}

}
